import request from '@/utils/request'

// 查询ldzl-qc列表
export function listIqc(query) {
  return request({
    url: '/ldzl-qc/qc/iqc/selectQcIqcList',
    method: 'get',
    params: query
  })
}

// 查询ldzl-qc详细
export function getIqc(iqcId) {
  return request({
    url: '/ldzl-qc/qc/iqc/selectQcIqcDetailed/' + iqcId,
    method: 'get'
  })
}

// 新增ldzl-qc
export function addIqc(data) {
  return request({
    url: '/ldzl-qc/qc/iqc/insertQcIqc',
    method: 'post',
    params: data,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  })
}

// 修改ldzl-qc
export function updateIqc(data) {
  return request({
    url: '/ldzl-qc/qc/iqc/updateQcIqc',
    method: 'post',
    data: data
  })
}

// 删除ldzl-qc
export function delIqc(iqcId) {
  return request({
    url: '/ldzl-qc/qc/iqc/deleteQcIqc/' + iqcId,
    method: 'post'
  })
}

// 查询检验模板列表
export function getQcTemplateList() {
  return request({
    url: '/ldzl-qc/qc/iqc/selectQcTemplateBasicInformation',
    method: 'post'
  })
}

// 查询模板下的要检测物料列表
export function getQcTemplateProductList(templateId, materialCode, materialName) {
  return request({
    url: '/ldzl-qc/qc/iqc/selectQcTemplateProductItemInformationTemplateId',
    method: 'post',
    params: {
      template_id: templateId,
      material_code: materialCode || '',
      material_name: materialName || ''
    }
  })
}

// 根据模板ID获取检测项列表
export function getQcTemplateIndexList(templateId) {
  return request({
    url: '/ldzl-qc/qc/iqc/selectQcTemplateIndexListByTemplateId',
    method: 'post',
    params: { template_id: templateId }
  })
}

// 防止生成重复物料的检测单，根据来源单据ID和物料编码判断是否重复，返回true表示不重复，返回false表示重复
export function checkIqcCodeUnique(sourceDocId, materialCode) {
  return request({
    url: '/ldzl-qc/qc/iqc/selectQcIqcCodeUnique',
    method: 'post',
    params: {
      source_doc_id: sourceDocId,
      material_code: materialCode
    }
  })
}

// 查询检测单的检查项信息
export function selectQcIqcLineByIqcId(data) {
  return request({
    url: '/ldzl-qc/qc/iqc/selectQcIqcLineByLineId',
    method: 'post',
    params: data,  // 使用params传参，与后端接口保持一致
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  })
}

// 添加检查项信息
export function insertQcIqcLine(data) {
  // 尝试多种方式传参，确保后端能接收到参数

  // 构建URL查询参数
  let queryParams = '';
  Object.keys(data).forEach(key => {
    if (data[key] !== null && data[key] !== undefined) {
      if (queryParams) queryParams += '&';
      queryParams += `${key}=${encodeURIComponent(data[key])}`;
    }
  });

  return request({
    url: `/ldzl-qc/qc/iqc/insertQcIqcLine?${queryParams}`,
    method: 'post',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  })
}

// 修改检查项信息
export function updateQcIqcLine(data) {
  // 构建URL查询参数
  let queryParams = '';
  Object.keys(data).forEach(key => {
    if (data[key] !== null && data[key] !== undefined) {
      if (queryParams) queryParams += '&';
      queryParams += `${key}=${encodeURIComponent(data[key])}`;
    }
  });

  return request({
    url: `/ldzl-qc/qc/iqc/updateQcIqcLine?${queryParams}`,
    method: 'post',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  })
}

// 批量删除检查项信息
export function deleteQcIqcLineByLineIds(lineIds) {
  return request({
    url: '/ldzl-qc/qc/iqc/deleteQcIqcLineByLineIds',
    method: 'post',
    params: { lineIds: lineIds }
  })
}

// 删除单个检查项信息
export function deleteQcIqcLineByLineId(lineId) {
  return request({
    url: '/ldzl-qc/qc/iqc/deleteQcIqcLineByLineId',
    method: 'post',
    params: { lineId: lineId }
  })
}

// 检验单详情
export function selectQcIqcDetailed(iqcCode) {
  console.log('调用API函数selectQcIqcDetailed，参数:', iqcCode);
  return request({
    url: '/ldzl-qc/qc/iqc/selectQcIqcDetailed',
    method: 'post',
    data: { iqc_code: iqcCode },  // 使用data传参而不是params
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    transformRequest: [(data) => {
      let ret = '';
      for (let it in data) {
        ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&';
      }
      return ret.slice(0, -1); // 去掉最后的&符号
    }]
  })
}

// 待检测入库单查询
export function selectCkProductRecptDetectSingle(params) {
   return request({
   url: `/ldzl-qc/qc/iqc/receipt/selectCkProductRecptDetectSingle`,
       method: 'get',
       params: params
     })
   }